package edu.whut.exception.handleException;

import edu.whut.exception.StudentAgeException;
import edu.whut.exception.domain.Student;

import java.util.Scanner;
/*
键盘录入学生姓名和年龄，封装为Student对象
 */
public class TryCatchTest {
    public static void main(String[] args) {
        Student stu = new Student();

        Scanner sc = new Scanner(System.in);

        System.out.println("输入姓名：");
        String name = sc.nextLine();
        stu.setName(name);
        System.out.println("输入年龄：");
        int age = 0;
        while (true) {
            try {
                age = Integer.parseInt(sc.nextLine());
                stu.setAge(age);
                break;
            } catch (NumberFormatException e) {
                System.out.println("年龄输入有误！");
            }catch(StudentAgeException e){
                String message = e.getMessage();
                System.out.println(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(stu);
    }
}
